//
//  DYPrintParse.m
//  DYPrintSDK
//
//  Created by mr.ye on 2023/11/22.
//

#import "DYPrintParse.h"

@implementation DYPrintParse

#pragma mark ---信息解析--
+(NSString *)hexStringWithData:(NSData *)data{
    Byte *bytes = (Byte *)[data bytes];
    NSString *hexStr=@"";
    for(int i=0;i<[data length];i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1){
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        }
        else{
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
        }
    }
    hexStr = [hexStr uppercaseString];
    return hexStr;
}


//基本解析信息
+ (NSMutableDictionary *)infoWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length >= 40) { // 设备信息
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSArray *arr = [str componentsSeparatedByString:@"|"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"mac"] = arr[1];
        dic[@"version"] = arr[3];
        dic[@"sn"] = arr[4];
        dic[@"power"] = arr[5];
        return dic;
    }
    return nil;
}

//检查打印机状态值
+ (NSInteger)parsePrinterStatus:(UInt8)statusByte {
    BOOL isPrinting = (statusByte & 0x01) != 0;
    BOOL isPaperCoverOpen = (statusByte & 0x02) != 0;
    BOOL isOutOfPaper = (statusByte & 0x04) != 0;
    BOOL isLowBattery = (statusByte & 0x08) != 0;
    BOOL isOverheating = (statusByte & 0x10) != 0;

    
    //正常状态
    if(!isPrinting && !isPaperCoverOpen && !isOutOfPaper && !isLowBattery && !isOverheating){
        return 0;
    }
    
//    LuckBLEManagerStatusOk=0,//正常
//    LuckBLEManagerStatusNoPaper=1,//没有纸
//    LuckBLEManagerStatusNoClose=2,//没关闭
//    LuckBLEManagerStatusPrinting=3,//打印中
//    LuckBLEManagerStatusLowBattery=4,//低电量
//    LuckBLEManagerStatusOverheating=5,//读取时间
    
    // 判断各个状态并输出
    if (isPrinting) {
//        NSLog(@"正在打印");
        return 3;
    }
    if (isPaperCoverOpen) {
//        NSLog(@"纸舱盖开");
        return 2;
    }
    if (isOutOfPaper) {
//        NSLog(@"打印机缺纸");
        return 1;
    }
    if (isLowBattery) {
//        NSLog(@"电池低电量");
        return 4;
       
    }
    if (isOverheating) {
//        NSLog(@"过热");
        return 5;
    }

    return 0;
}


//解析打印结束
+ (BOOL)imageCompeleteWithData:(NSData *)data {
    NSString *recivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString * text = [self hexStringWithData:data];
    NSLog(@"打印状态----%@====%@",text,recivedString);
    if([recivedString containsString:@"OK"] || [text containsString:@"AA"]){
        return YES;
    }
    return NO;
}
//解析时间
+ (NSUInteger)closeTimeWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length == 2) {
        if (list[0] == 0x00) {
            return list[1] + 1;
        }
    }
    if (data.length == 1) {
        return list[0] + 1;
    }
    return 0;
}
//解析型号
+ (NSString *)mdStringWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length >= 2) {
        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return nil;
}
//解析信号
+ (NSUInteger)creditWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length == 2) {
        if (list[0] == 0x01) {
            return list[1];
        }
    }
    return 0;
}
//解析MTU
+ (NSUInteger)mtuWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length == 3) {
        if (list[0] == 0x02) {
            return list[1] + (list[2] << 8);
        }
    }
    return 0;
}

//解析浓度
+ (NSUInteger)thickWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length == 3) {
        return list[0] + 1;
    }
    return 0;
}
//解析更新时候的信号值
+ (NSUInteger)updateCreditWithData:(NSData *)data {
    Byte list[data.length];
    [data getBytes:&list length:data.length];
    if (data.length == 14) {
        if (list[0] == 0x1B && list[1] == 0x10) {
            return 1;
        }
    }
    return 0;
}

//计算Eec
+ (Byte)calculateEnc:(Byte *)data length:(NSUInteger)length {
    Byte result = 0;
    for (NSUInteger i = 0; i < length; i++) {
        result += data[i];
    }
    return result;
}


+ (NSData *)intToByteArray4:(NSUInteger)inter length:(NSUInteger)length {
    
    Byte list[length];
    
    for (int i = 0; i < length; i++) {
        NSUInteger move = (length - i - 1) * 8;
        list[i] = (inter >> move) & 0xFF;
    }
    NSData *data = [NSData dataWithBytes:list length:length];
    return data;
}

@end
